//
//  DetailViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/22.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "DetailViewController.h"
#import "common.h"
#import "DetailCell.h"
#import "News.h"
#import "DataService.h"
#import "PhototViewController.h"

@interface DetailViewController () <UINavigationControllerDelegate>



@end

@implementation DetailViewController



- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat size = (kWidth - 46) / 4;
    layout.itemSize = CGSizeMake(size, size);
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 15;
    _detail = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    _detail.dataSource = self;
    _detail.delegate = self;
    [_detail registerNib:[UINib nibWithNibName:detailCell bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:detailCell];
    [self.view addSubview:_detail];
}


- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_data count];
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    DetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:detailCell forIndexPath:indexPath];
    cell.model = _data[indexPath.row];
    return cell;
}

- (void)collectionView:(nonnull UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    PhototViewController *photo = [[PhototViewController alloc] init];
    for (News *new in _data) {
        NSString *imageUrl = new.image;
        [array addObject:imageUrl];
    }
    photo.view.backgroundColor = [UIColor redColor];
    photo.imageUrl = array;
    photo.currentImage = indexPath.row;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:photo];
    nav.navigationBar.translucent = YES;
    
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self _loadData];
    [self createCollectionView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)_loadData {
    _data = [[NSMutableArray alloc] init];
    NSArray *array = [DataService readJsonFlieWithNamed:detailJson];
    for (NSDictionary *dic in array) {
        News *new = [[News alloc] init];
        new.image = [dic objectForKey:@"image"];
        [_data addObject:new];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
